/*
 * MainGUI.java
 *
 * Created on October 19, 2009, 4:28 PM
 */

package gui;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import alg.*;

/**
 *
 * @author  maxime
 */
@SuppressWarnings("serial")
public class MainGUI extends JFrame
{

    private final JFileChooser fc = new JFileChooser();
    /** Creates new form MainGUI */
    public MainGUI()
    {
        setTitle("Knight School Schedule Generator");
        setResizable(false);
        initComponents();
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     */
    @SuppressWarnings("unchecked")
    private void initComponents()
    {

    	studentsLabel = new JLabel();
        studentsTextField = new JTextField();
        coursesLabel = new JLabel();
        coursesTextField = new JTextField();
        studentsBrowseButton = new JButton();
        coursesBrowseButton = new JButton();
        generateScheduleButton = new JButton();
        outputLabel = new JLabel();
        outputTextField = new JTextField();
        outputBrowseButton = new JButton();
        outputScrollPane = new JScrollPane();
        outputTextArea = new JTextArea();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        studentsLabel.setText("Students File: ");

        studentsTextField.setText("Knight_School_Signups.csv");
        studentsTextField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                studentsTextFieldActionPerformed(evt);
            }
        });

        coursesLabel.setText("Courses File: ");

        coursesTextField.setText("KS_Course_Caps.csv");
        coursesTextField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                coursesTextFieldActionPerformed(evt);
            }
        });

        studentsBrowseButton.setText("Browse...");
        studentsBrowseButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                studentsBrowseButtonActionPerformed(evt);
            }
        });

        coursesBrowseButton.setText("Browse...");
        coursesBrowseButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                coursesBrowseButtonActionPerformed(evt);
            }
        });

        generateScheduleButton.setText("Generate Schedule");
        generateScheduleButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                generateScheduleButtonActionPerformed(evt);
            }
        });

        outputLabel.setText("Output File: ");

        outputTextField.setText("out.txt");

        outputBrowseButton.setText("Browse...");
        outputBrowseButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                outputBrowseButtonActionPerformed(evt);
            }
        });

        outputTextArea.setColumns(20);
        outputTextArea.setEditable(false);
        outputTextArea.setRows(5);
        outputScrollPane.setViewportView(outputTextArea);

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addComponent(outputScrollPane, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                            .addComponent(outputLabel, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(coursesLabel, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(studentsLabel, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                            .addComponent(outputTextField)
                            .addComponent(coursesTextField, GroupLayout.Alignment.TRAILING)
                            .addComponent(studentsTextField, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(coursesBrowseButton, GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                            .addComponent(studentsBrowseButton, GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                            .addComponent(outputBrowseButton, GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)))
                    .addComponent(generateScheduleButton, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(studentsLabel)
                    .addComponent(studentsTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(studentsBrowseButton))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(coursesLabel)
                    .addComponent(coursesTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(coursesBrowseButton))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(outputLabel)
                    .addComponent(outputTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(outputBrowseButton))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(generateScheduleButton, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(outputScrollPane, GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }
    
    private void studentsTextFieldActionPerformed(ActionEvent evt)
    {
    	
    }

    private void generateScheduleButtonActionPerformed(ActionEvent evt)
    {
    	AlgorithmTester.run(outputTextField.getText(), coursesTextField.getText(), studentsTextField.getText());
    	outputTextArea.append(AlgorithmTester.getStatistics());
    	outputTextArea.append("---------------------------------");
    	outputTextArea.append("\n");
    }

    private void coursesTextFieldActionPerformed(ActionEvent evt)
    {
    	
    }
    
    private void coursesBrowseButtonActionPerformed(ActionEvent evt)
    {
    	// TODO add your handling code here:
    	int returnVal = fc.showOpenDialog(this);
    	
    	if (returnVal == JFileChooser.APPROVE_OPTION)
    	{
    		File file = fc.getSelectedFile();
    		this.coursesTextField.setText(file.getAbsolutePath());
    	}
    }

    private void outputBrowseButtonActionPerformed(ActionEvent evt)
    {
    	// 	TODO add your handling code here:
    	int returnVal = fc.showOpenDialog(this);
    	
    	if (returnVal == JFileChooser.APPROVE_OPTION)
    	{
    		File file = fc.getSelectedFile();
    		this.outputTextField.setText(file.getAbsolutePath());
    	}
    }
    
    private void studentsBrowseButtonActionPerformed(ActionEvent evt)
    {
    	// 	TODO add your handling code here:
    	int returnVal = fc.showOpenDialog(this);
    	
    	if (returnVal == JFileChooser.APPROVE_OPTION)
    	{
    		File file = fc.getSelectedFile();
    		this.studentsTextField.setText(file.getAbsolutePath());
    	}
    }

    // Variables declaration - do not modify
    private JButton coursesBrowseButton;
    private JLabel coursesLabel;
    private JTextField coursesTextField;
    private JButton generateScheduleButton;
    private JButton outputBrowseButton;
    private JLabel outputLabel;
    private JTextField outputTextField;
    private JButton studentsBrowseButton;
    private JLabel studentsLabel;
    private JTextField studentsTextField;
    private JScrollPane outputScrollPane;
    private JTextArea outputTextArea;
}
